/*
 * Created on Nov 15, 2003 Local Projects Database Mihai Postelnicu
 *  
 */
package ro.gateway.aida.struts.action;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.OrgType;
import ro.gateway.aida.obj.Organization;
import ro.gateway.aida.obj.db.OrgTypeDB;
import ro.gateway.aida.obj.db.OrganizationDB;
import ro.gateway.aida.struts.form.OrgSelectorForm;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.BugTracker;
import ro.gateway.aida.utils.LocalisedSortableList;
import ro.gateway.aida.utils.PageToken;
import ro.xblue.translator.TranslatorUtils;
/**
 * @author Mihai Postelnicu OrgSelectorAction
 *  
 */
public class OrgSelectorAction extends TilesAction {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			OrgSelectorForm osf = (OrgSelectorForm) form;
			ServletContext sc = getServlet().getServletContext();
			PersistenceToken token = PersistenceManager.tokenLookup(sc);
			response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
			response.setHeader("Pragma", "no-cache"); //HTTP 1.0
			response.setDateHeader("Expires", -1);
			Arbiter ar = Arbiter.getManager(token);
			/*if (UserUtils.getLogged(request, response) == null)
				return mapping.findForward("plslogin");*/
			/*if (!ar.canDo(request, Perms.OBJECT_SUBMIT))
				mapping.findForward("norights");*/
			HttpSession session = request.getSession();
			String lang = TranslatorUtils.getCurrentLanguage(request, sc)
					.getLanguage();
			if (request.getParameter("hidequickadd") != null)
				request.setAttribute("hidequickadd", "y");
			if (request.getParameter("quickadd") != null) {
				return mapping.findForward("quickadd");
			}
			if (request.getParameter("clear") != null) {
				session.removeAttribute(SEARCH_RESULTS);
				return mapping.getInputForward();
			}
			if (request.getParameter("cancel") != null) {
				request.setAttribute("cancel", "cancel");
				session.removeAttribute(SEARCH_RESULTS);
				return mapping.getInputForward();
			}
			OrgTypeDB otdb = OrgTypeDB.getManager(token);
			OrganizationDB odb = OrganizationDB.getManager(token);
			if (request.getParameter("quicksubmit") != null) {
				Organization o = new Organization();
				if (osf.getQuickName().equals(""))
					return mapping.findForward("quickadd");
				o.getName().setString(lang, osf.getQuickName());
				o.getDescription().setString(lang, osf.getQuickComment());
				o.setCatID(osf.getQuickType());
				o.setUserID(UserUtils.getLogged(request).getId());
				o.setTemporary(true);
				odb.insert(o, lang);
				LocalisedSortableList sl = new LocalisedSortableList(lang,
						new Organization.LangComparator(lang));
				sl.add(o);
				session.setAttribute(SEARCH_RESULTS, sl);
				List searchResults = (List) session.getAttribute(SEARCH_RESULTS);
				PageToken.tokenizeList(request, sc, searchResults,
						"OrgSearchResults");
				
				
				return mapping.getInputForward();
			}
			if (session.getAttribute(OrgType.LIST) == null)
				session.setAttribute(OrgType.LIST, otdb.getAllLocalized(lang));
			else {
				((LocalisedSortableList) session.getAttribute(OrgType.LIST))
						.setViewLang(lang);
			}
			if (request.getParameter("add") != null) {
				ArrayList selectedOrgs = new ArrayList();
				List searchResults = (List) session
						.getAttribute(SEARCH_RESULTS);
				if (searchResults != null) {
					Iterator it = searchResults.iterator();
					while (it.hasNext()) {
						Organization element = (Organization) it.next();
						for (int i = 0; (osf.getSelectedOrgs() != null)
								&& (i < osf.getSelectedOrgs().length); i++)
							if (element.getId() == osf.getSelectedOrgs()[i])
								selectedOrgs.add(element);
					}
					session.removeAttribute(SEARCH_RESULTS);
					session.setAttribute("SelectedOrgs", selectedOrgs);
					request.setAttribute("submit", "submit");
				}
			}
			if (request.getParameter("search") != null) {
				List searchResults = null;
				if ((osf.getTextSearch().length() != 0)
						&& (osf.getTypeSearch() != -1))
					searchResults = odb.getByLikeAndType(osf.getTextSearch(),
							osf.getTypeSearch());
				else if (osf.getTextSearch().length() != 0)
					searchResults = odb.getByLike(osf.getTextSearch());
				else if (osf.getTypeSearch() != -1)
					searchResults = odb.getByType(osf.getTypeSearch());
				else
					searchResults = odb.getList();
				LocalisedSortableList sl = new LocalisedSortableList(lang,
						new Organization.LangComparator(lang), searchResults);
				session.setAttribute(SEARCH_RESULTS, sl);
				if (sl.size() == 0)
					session.removeAttribute(SEARCH_RESULTS);
			}
			List searchResults = (List) session.getAttribute(SEARCH_RESULTS);
			PageToken.tokenizeList(request, sc, searchResults,
					"OrgSearchResults");
			return mapping.getInputForward();
		} catch (Exception e) {
			BugTracker.publishBug(e, request);
			return mapping.findForward("fail");
		}
	}
	private static final String SEARCH_RESULTS = "OrgSearchResults";
}